package com.max.common.util;

import java.util.StringTokenizer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonUtil 
{

	public static <T> T getValue(JSONObject json, String key, T defaults)
	{
		T t = null;
		
		if(json != null && !StringUtil.isEmpty(key))
		{
			StringTokenizer st = new StringTokenizer(key, ".");
			JSONObject jsonTmp = json;
			
			try
			{
				while(st.hasMoreTokens())
				{
					String token = st.nextToken();
					
					if(st.hasMoreTokens())
						jsonTmp = jsonTmp.getJSONObject(token);
					else 
						t = convert(jsonTmp.get(token).toString(), defaults);
				}
			}
			catch(Exception e)
			{
				e.printStackTrace();
				t = defaults;
			}
		}
		else
			t = defaults;
		
		return t;
	}
	
	
	public static JSONObject getJSONObject(JSONObject json, String key)
	{
		JSONObject retJson = null;
		
		if(json != null && !StringUtil.isEmpty(key))
		{
			StringTokenizer st = new StringTokenizer(key, ".");
			
			try
			{
				while(st.hasMoreTokens())
				{
					String token = st.nextToken();
					
					retJson = json.getJSONObject(token);
				}
			}
			catch(Exception e)
			{
				e.printStackTrace();
				retJson = null;
			}
		}
		
		return retJson;
	}
	
	
	public static JSONArray getJSONArray(JSONObject json, String key)
	{
		JSONArray jsonArray = null;
		
		if(json != null && !StringUtil.isEmpty(key))
		{
			StringTokenizer st = new StringTokenizer(key, ".");
			JSONObject jsonTmp = json;
			
			try
			{
				while(st.hasMoreTokens())
				{
					String token = st.nextToken();
					
					if(st.hasMoreTokens())
						jsonTmp = jsonTmp.getJSONObject(token);
					else
						jsonArray = jsonTmp.getJSONArray(token);
				}
			}
			catch(Exception e)
			{
//				e.printStackTrace();
				jsonArray = null;
			}
		}
		
		return jsonArray;
	}
	
	
	@SuppressWarnings("unchecked")
	public static <T> T convert(String str, T defaults)
    {
        T t = null;
        
        if(str != null)
        {
        	try
        	{
        		if (String.class.isAssignableFrom(defaults.getClass()))
            		t = (T) str;
            	else if (Integer.class.isAssignableFrom(defaults.getClass()))
            		t = (T) Integer.valueOf(str);
            	else if (Long.class.isAssignableFrom(defaults.getClass()))
            		t = (T) Long.valueOf(str);
            	else if (Short.class.isAssignableFrom(defaults.getClass()))
            		t = (T) Short.valueOf(str);
            	else if (Character.class.isAssignableFrom(defaults.getClass()))
            		t = (T) str;
            	else if (Byte.class.isAssignableFrom(defaults.getClass()))
            		t = (T) Byte.valueOf(str);
            	else if (Boolean.class.isAssignableFrom(defaults.getClass()))
            		t = (T) Boolean.valueOf(str);
            	else if (Double.class.isAssignableFrom(defaults.getClass()))
            		t = (T) Double.valueOf(str);
            	else if (Float.class.isAssignableFrom(defaults.getClass()))
            		t = (T) Float.valueOf(str);
            	else
            		t = defaults;
        	}
        	catch(Exception e)
        	{
        		e.printStackTrace();
        		t = defaults;
        	}
        }
        else
        	t = defaults;
        
        return t;
    }
	
	
	public static void main(String [] args) throws JSONException
	{
		JSONObject json = new JSONObject("{\"data\":{ \"hasnext\":1,\"info\":[ {\"city_code\":\"wuhan\",\"count\":0,\"country_code\":\"1\"}] }}   ");
		System.out.println(getValue(json,"data.hasnext",0));
		
	}
}
